package com.sysd.mail.smtp.command;

import com.sysd.mail.entity.bean.User;
import com.sysd.mail.smtp.SmtpSession;

import java.io.IOException;
import java.util.Locale;

/**
 * project: mail
 *
 * @author bsh
 * @description AuthCommand
 * @dataTime 2022/12/30 14:48
 * @company 丽水信阳软件科技有限公司
 */
public class AuthCommand extends BaseSmtpCommand {
    public AuthCommand() {
        super("AUTH");
    }

    /**
     * 执行命令
     *
     * @param command     命令名称
     * @param smtpSession smtp session
     * @return smtp session
     * @throws IOException io exception
     */
    @Override
    public SmtpSession execute(String command, SmtpSession smtpSession) throws IOException {
        smtpSession.setUser(new User());
        String[] args = this.getCommandNameAndArgs(command);
        if(args.length<2){
            smtpSession.write("502 \r\n");
            return smtpSession;
        }
        if ("LOGIN".equals(args[1].toUpperCase(Locale.ENGLISH)))
        {
            smtpSession.write("334 VXNlcm5hbWU6\r\n");
            smtpSession.setLoginProcess(true);
            smtpSession.setLoginProcessCount(smtpSession.getLoginProcessCount()+1);
        }
        else {
            smtpSession.write("504 \r\n");
        }
        return smtpSession;
    }
}
